package com.ruoyi.merchant.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.db.domain.MerchantBaseInfo;
import com.ruoyi.db.domain.MerchantOperatorLog;
import com.ruoyi.db.mapper.AddressInfoMapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.db.mapper.MerchantBaseInfoMapper;
import com.ruoyi.db.mapper.MerchantOperatorLogMapper;
import com.ruoyi.laiqianba.enums.InfoTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.MerchantTaskInfoMapper;
import com.ruoyi.db.domain.MerchantTaskInfo;
import com.ruoyi.merchant.service.IMerchantTaskInfoService;

import javax.annotation.Resource;

/**
 * 商户推送任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-12
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class MerchantTaskInfoServiceImpl implements IMerchantTaskInfoService {
    @Resource
    private MerchantTaskInfoMapper merchantTaskInfoMapper;

    @Resource
    private AddressInfoMapper addressInfoMapper;

    @Resource
    private MerchantBaseInfoMapper merchantBaseInfoMapper;

    @Resource
    private MerchantOperatorLogMapper merchantOperatorLogMapper;


    /**
     * 查询商户推送任务
     *
     * @param id 商户推送任务主键
     * @return 商户推送任务
     */
    @Override
    public MerchantTaskInfo selectMerchantTaskInfoById(Long id) {
        return merchantTaskInfoMapper.selectMerchantTaskInfoById(id);
    }

    /**
     * 查询商户推送任务列表
     *
     * @param merchantTaskInfo 商户推送任务
     * @return 商户推送任务
     */
    @Override
    public List<MerchantTaskInfo> selectMerchantTaskInfoList(MerchantTaskInfo merchantTaskInfo) {
        return merchantTaskInfoMapper.selectMerchantTaskInfoList(merchantTaskInfo);
    }

    @Override
    public List<MerchantTaskInfo> selectTaskInfoList(MerchantTaskInfo merchantTaskInfo) {

        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            Long deptId = SecurityUtils.getDeptId();
            Long InfoId = merchantBaseInfoMapper.seletInfoIdBydeptId(deptId);
            merchantTaskInfo.setInfoId(InfoId);
        }
        return merchantTaskInfoMapper.selectTaskInfoList(merchantTaskInfo);
    }

    /**
     * 新增商户推送任务
     *
     * @param merchantTaskInfo 商户推送任务
     * @return 结果
     */
    @Override
    public int insertMerchantTaskInfo(MerchantTaskInfo merchantTaskInfo) {

        String cityInfo = merchantTaskInfo.getCityInfo();
        if (StringUtils.isNotEmpty(cityInfo)) {
            JSONArray jsonArray = JSON.parseArray(cityInfo);
            List<String> stringList = jsonArray.toJavaList(String.class);

            String codeValue = String.join(",", stringList);
            List<String> descinfo = addressInfoMapper.selectAddressNamesByCodeValues(codeValue);
            merchantTaskInfo.setDescInfo(descinfo.toString());

            if (codeValue.contains("999999")) {
                merchantTaskInfo.setPushType(2L);
            } else {
                merchantTaskInfo.setPushType(1L);
            }
        }
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoMapper.selectMerchantBaseInfoById(merchantTaskInfo.getInfoId());
        merchantTaskInfo.setInfoType(merchantBaseInfo.getInfoType());
        if (StrUtil.equals(InfoTypeEnum.APIS.getValue(), merchantBaseInfo.getInfoType())) {
            merchantTaskInfo.setStarttime("00:00:00");
            merchantTaskInfo.setEndtime("23:59:59");
        }

        merchantTaskInfo.setCreateTime(DateUtils.getNowDate());

        if (merchantTaskInfo.getPayAmount() != null) {
            merchantTaskInfo.setPriceAmount(merchantTaskInfo.getCostAmount().add(merchantTaskInfo.getPayAmount()));
            return merchantTaskInfoMapper.insertMerchantTaskInfo(merchantTaskInfo);
        }
        merchantTaskInfo.setPriceAmount(merchantTaskInfo.getCostAmount());


        return merchantTaskInfoMapper.insertMerchantTaskInfo(merchantTaskInfo);
    }


    /**
     * 修改商户推送任务
     *
     * @param merchantTaskInfo 商户推送任务
     * @return 结果
     */
    @Override
    public int updateMerchantTaskInfo(MerchantTaskInfo merchantTaskInfo) {

        String cityInfo = merchantTaskInfo.getCityInfo();
        if (StringUtils.isNotEmpty(cityInfo)) {
            JSONArray jsonArray = JSON.parseArray(cityInfo);
            List<String> stringList = jsonArray.toJavaList(String.class);

            String codeValue = String.join(",", stringList);
            List<String> descinfo = addressInfoMapper.selectAddressNamesByCodeValues(codeValue);
            merchantTaskInfo.setDescInfo(descinfo.toString());

            if (codeValue.contains("999999")) {
                merchantTaskInfo.setPushType(2L);
            } else {
                merchantTaskInfo.setPushType(1L);
            }
        }
        merchantTaskInfo.setUpdateTime(DateUtils.getNowDate());


        MerchantTaskInfo TaskInfo1 = merchantTaskInfoMapper.selectMerchantTaskInfoById(merchantTaskInfo.getId());
        MerchantOperatorLog merchantOperatorLog = new MerchantOperatorLog();
        merchantOperatorLog.setInfoId(TaskInfo1.getInfoId());
        merchantOperatorLog.setTiskId(merchantTaskInfo.getId());
        merchantOperatorLog.setOperatorId(SecurityUtils.getUserId());
        merchantOperatorLog.setOperatorName(SecurityUtils.getUsername());
        merchantOperatorLog.setType(1L); // 0代表是任务
        merchantOperatorLog.setOperationType(merchantTaskInfo.getStatus());
        merchantOperatorLog.setCreateTime(DateUtils.getNowDate());
        merchantOperatorLogMapper.insertMerchantOperatorLog(merchantOperatorLog);


        if (merchantTaskInfo.getPayAmount() != null) {
            merchantTaskInfo.setPriceAmount(merchantTaskInfo.getCostAmount().add(merchantTaskInfo.getPayAmount()));
            return merchantTaskInfoMapper.updateMerchantTaskInfo(merchantTaskInfo);
        }


        merchantTaskInfo.setPriceAmount(merchantTaskInfo.getCostAmount());
        return merchantTaskInfoMapper.updateMerchantTaskInfo(merchantTaskInfo);
    }

    /**
     * 批量删除商户推送任务
     *
     * @param ids 需要删除的商户推送任务主键
     * @return 结果
     */
    @Override
    public int deleteMerchantTaskInfoByIds(Long[] ids) {
        return merchantTaskInfoMapper.deleteMerchantTaskInfoByIds(ids);
    }

    /**
     * 删除商户推送任务信息
     *
     * @param id 商户推送任务主键
     * @return 结果
     */
    @Override
    public int deleteMerchantTaskInfoById(Long id) {
        return merchantTaskInfoMapper.deleteMerchantTaskInfoById(id);
    }


}
